package com.syq.play.util;

import java.io.File;
import java.io.FileFilter;
import java.util.HashSet;
import java.util.Set;
import java.util.Vector;

import com.syq.play.db.entity.SongManager;
import com.syq.play.id3.Id3Util;
import com.syq.play.model.MediaModel;

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

/**
 * <b>文件过滤扫描类</b>
 * 
 */
public class FileScanTool {
	public final static String SDCARD = Environment
			.getExternalStorageDirectory()
			+ "/";
	public  Set<String> fileStr=new HashSet<String>();
	private TextView tvSong;
	
	
	private Handler mHandler=new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Bundle data=msg.getData();
			String song=data.getString("song");
			tvSong.setText(song);
		}
	};
	
	/**
	 * 扫描根目录为SDCARD，的文件
	 * @param context
	 * @param filterName  扫描文件后缀
	 */
	public void scanFile(final Context context,  final String filterName, TextView tvSong){
		scanFile(context, SDCARD, filterName, tvSong);
	}
	
	/**
	 * @param rootPath
	 *            根目录
	 * @param fileName
	 *            扫描文件后缀
	 * */
	public  void  scanFile(final Context context, String path, final String filterName, final TextView tvSong) {
		this.tvSong=tvSong;
		File rootPath=new File(path);
		rootPath.listFiles(new FileFilter() {

			@Override
			public boolean accept(File pathname) {
				String path=pathname + "";
				if (((path).toLowerCase()).endsWith(filterName)) {
					fileStr.add(path);
					Log.i("syq", "fileStr="+path);
					MediaModel media=Id3Util.getMp3TAG(context, path);
					SongManager.getInstance().insetSongData(media);
					Message msg=new Message();
					Bundle data=new Bundle();
					data.putString("song", path.substring(path.lastIndexOf("/")+1));
					msg.setData(data);
					mHandler.sendMessage(msg);
					return true;
				}
				if (pathname.isDirectory()) {
					scanFile(context, path, filterName, tvSong);
					return true;
				} else {
					return false;
				}
			}
		});
	}

	/**
	 * 多格式扫描
	 * 
	 * @param rootPath
	 *            根目录
	 * @param filterNames
	 *            格式数组
	 * */
//	public void scanFile(File rootPath, final String[] filterNames) {
//		for (int i = 0; i < filterNames.length; i++) {
//			scanFile(rootPath, filterNames[i]);
//		}
//	}
	public static boolean haveFile(String path){
		File dirf=new File(path);
	     if(dirf.exists()){
	    	 return true;
	     }
	     return false;
	}


}
